/* eslint-disable */
const Controller = require('egg').Controller;

class AdminController extends Controller {
    async register() {
        const { ctx } = this;
        const { username, password,role } = ctx.request.body; 

        const valid = await ctx.service.admin.checkAdminNameValid(username);
        if (valid) {
            const result = await ctx.service.admin.add({ username, password,role });
            ctx.success(result, '管理员注册成功');
        } else {
            ctx.error(`用户名为${username}的管理员已存在，不能再注册！`);
        }
    }
    async login() {
        const { ctx } = this;
        const { username, password, role,id } = ctx.request.body;

        const result = await ctx.service.admin.login(username, password, role,id);
        if (result.success) {
            ctx.success(result.data, '登录成功');
        } else {
            ctx.error(result.message);
        }
    }
}

module.exports = AdminController;
